package cn.org.timespace.ons.action.base;

import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.org.timespace.easyframe.action.EFBaseAction;
import cn.org.timespace.ons.service.base.AppleServerService;
import cn.org.timespace.ons.service.base.MachinesApplyService;
import cn.org.timespace.ons.service.base.MachinesService;
import cn.org.timespace.ons.util.pager.Pager;
import cn.org.timespace.ons.util.pager.PagerHelper;
import cn.org.timespace.ons.vo.base.AppleServer;
import cn.org.timespace.ons.vo.base.Machines;
import cn.org.timespace.ons.vo.base.MachinesApply;
import cn.org.timespace.ons.vo.base.Options;

public class MachinesApplyAction extends EFBaseAction {

	private static final long serialVersionUID = -3416183313291298070L;
	private static Log log=LogFactory.getLog(MachinesApplyAction.class);
	private MachinesApplyService machinesApplyService; 
	private AppleServerService appleServerService;
	private MachinesService machinesService;
	
	
	
	public MachinesService getMachinesService() {
		return machinesService;
	}
	public void setMachinesService(MachinesService machinesService) {
		this.machinesService = machinesService;
	}
	public AppleServerService getAppleServerService() {
		return appleServerService;
	}
	public void setAppleServerService(AppleServerService appleServerService) {
		this.appleServerService = appleServerService;
	}
	public MachinesApplyService getMachinesApplyService() {
		return machinesApplyService;
	}
	public void setMachinesApplyService(MachinesApplyService machinesApplyService) {
		this.machinesApplyService = machinesApplyService;
	}
	
	public MachinesApplyAction()
	{
		super(new MachinesApplyModel());
	}
	MachinesApplyModel machinesApplyModel = (MachinesApplyModel)this.getModel();
	
	
	
	
	/**
	 * 根据信息终端Id 查询出对应的machinesApply列表信息
	 * @return
	 * @throws Exception
	 */
	public String doList() throws Exception{
		Pager p = PagerHelper.getPagerInstance(getRequest());
		String msid = getRequest().getParameter("msid") ;
		String name = machinesApplyModel.getName();
		//List<Object> resList = machinesApplyService.findByMachineId(msid, p);
		List<AppleServer> resList = machinesApplyService.findByMsidAndName(msid, name, p);
		getRequest().setAttribute("resList", resList);
		getRequest().setAttribute("msid", msid);
		return LIST;
	}
	
	/**
	 * 根据应用Id 查询出对应的machinesApply列表信息
	 * @return
	 */
	public String doAppList(){
		Pager p = PagerHelper.getPagerInstance(getRequest());
		String asId = getRequest().getParameter("asId") ;
		String name = machinesApplyModel.getName();
		//根据应用ID查找信息终端
		//List<Object> resList = machinesApplyService.findByApplyId(asId, p);
		List<Options> resList = machinesApplyService.findByAppIdAndName(asId, name, p);
		getRequest().setAttribute("resList", resList);
		
		getRequest().setAttribute("asId", asId);
		return "listApp";
	}
	
	
	
	/**
	 * 更新终端应用
	 * @return
	 * @throws Exception
	 */
	public String doUpdate() throws Exception {
		MachinesApply machinesApply = machinesApplyModel.getMachinesApply();
		
		machinesApplyService.update(machinesApply);
		return doList();
	}
	
	/**
	 * 保存选择的应用
	 * @return
	 * @throws Exception
	 */
	public String doSave() throws Exception{
		String addString = getRequest().getParameter("addString");
		PrintWriter out;
		this.getResponse().setCharacterEncoding("UTF-8");
		String[] mid = addString.split(",");
		String msid = getRequest().getParameter("msid");
		boolean flag = false;
		if(mid.length>0){
			//String msid = asid[asid.length - 1];
			MachinesApply machinesApply = new MachinesApply();
		//	System.out.println(msid);
			for(int i = 0 ; i<mid.length ; i++){
				AppleServer appleServer = appleServerService.findById(mid[i]);
				int status = appleServer.getStatus();
				
				machinesApply.setMsid(msid);
				machinesApply.setAsid(mid[i]);
				machinesApply.setName(appleServer.getName());
				machinesApply.setStatus(status);
				machinesApply.setRemark(appleServer.getRemark());
				machinesApply.setSentTime(new Timestamp(System.currentTimeMillis()));
				//增加发起人和审核人
				machinesApply.setStartUserName(getUserAccount());
				machinesApply.setEndUserName(appleServer.getUserName());
				flag = machinesApplyService.save(machinesApply);
			}
			//getRequest().setAttribute("msid", msid);
			//returnList(msid , "app");
			if(flag){
				out = getResponse().getWriter();
				out.write("1");
			}
		}
		return null;
	}
	
	
	/**
	 * 保存选择的终端
	 * @return
	 */
	public String doSaveMachine() throws Exception{
		String addString = getRequest().getParameter("addString");
		log.info(addString);
		PrintWriter out;
		this.getResponse().setCharacterEncoding("UTF-8");
		String[] mid = addString.split(",");
		String asId = getRequest().getParameter("asid");
		boolean flag = false;
		if(mid.length>0){
			//String asId = mid[mid.length - 1];//页面传过来的字符串，前面那部分是终端id，最后一个是应用id (一个应用 --多个终端)
			MachinesApply machinesApply = new MachinesApply();
			for(int i = 0 ; i<mid.length ; i++){
				Machines machines = machinesService.findById(mid[i]);
				int status = machines.getStatus();
				String remark = machines.getRemark();
				String name = machines.getName();
				//System.out.println("status:"+status+"   remark:"+remark+"    name:"+name);
				machinesApply.setMsid(mid[i]);
				machinesApply.setAsid(asId);
				machinesApply.setName(name);
				machinesApply.setStatus(status);
				machinesApply.setRemark(remark);
				machinesApply.setSentTime(new Timestamp(System.currentTimeMillis()));
				//增加发起人和审核人
				machinesApply.setStartUserName(getUserAccount());
				machinesApply.setEndUserName(machines.getUserName());
				flag = machinesApplyService.save(machinesApply);
			}
			//getRequest().setAttribute("asId", asId);
			//returnList(asId , "machine");
			if(flag){
				out = getResponse().getWriter();
				out.write("1");
			}
		}
		
		 
		return null;
	}
	
	
	public String doDelete() throws Exception{
		
		String machinesId = getRequest().getParameter("machinesId") ;
		MachinesApply machinesApply = machinesApplyService.findById(machinesId);
		machinesApplyService.delete(machinesApply);
		return doList();
	}
	
	/**
	 * 显示appleserver列表,该页面可以选择要添加的应用
	 * @return
	 * @throws Exception
	 */
	public String doAddApply() throws Exception{
		
		Pager p = PagerHelper.getPagerInstance(getRequest());
		
		
		List<Object> resList = appleServerService.findByCondition(p);
		
		
		String msid = getRequest().getParameter("msid");
		
		
		
		if(resList != null && resList.size() == 2){
			p.setTotalCount(((Integer)resList.get(0)));
			p.setData((List)resList.get(1));
		}
		getRequest().setAttribute("msid", msid);
		return ADD;
	}
	
	
	/**
	 * 显示machines列表,该页面可以选择要添加的终端
	 * @return
	 * @throws Exception
	 */
	public String doAddMachine() throws Exception{
		
		Pager p = PagerHelper.getPagerInstance(getRequest());
		List<Object> resList = machinesService.findByCondition(p);
		String asId = getRequest().getParameter("asId");
		if(resList != null && resList.size() == 2){
			p.setTotalCount(((Integer)resList.get(0)));
			p.setData((List)resList.get(1));
		}
		getRequest().setAttribute("asId", asId);
		return "addMachine";
	}
	
	
	/**
	 * 显示我的申请
	 * @return
	 */
	public String doStartList() throws Exception{
		
		Pager p = PagerHelper.getPagerInstance(getRequest());
		MachinesApply machinesApply = new MachinesApply();
		machinesApply.setStartUserName(getUserAccount());
		List<Object> resList = machinesApplyService.findApplyAndAuditByCondition(machinesApply, p);
		
		if(resList != null && resList.size() == 2){
			p.setTotalCount(((Integer)resList.get(0)));
			p.setData((List)resList.get(1));
		}
		
		return "myApply";
	}
	
	/** 0审核通过
	 * 2审核不通过
	 * 3取消我的申请
	 */
	public String doCanceApply()throws Exception{
		
		String maid = getRequest().getParameter("maid") ;
		String status = getRequest().getParameter("status") ;
		MachinesApply machinesApply = machinesApplyService.findById(maid);
         
        machinesApply.setStatus(Integer.valueOf(status));
		machinesApplyService.update(machinesApply);
		
		if(status.equals("0")||status.equals("2")){
			return "myAudit";
		}else if(status.equals("3")){
			return "myApply";
		}
		
		return null;
	}
	/**
	 * 我的审核
	 */
	public String doMyAuditList()throws Exception{
		
		Pager p = PagerHelper.getPagerInstance(getRequest());
		MachinesApply machinesApply = new MachinesApply();
		machinesApply.setEndUserName(getUserAccount());
		List<Object> resList = machinesApplyService.findApplyAndAuditByCondition(machinesApply, p);
		
		if(resList != null && resList.size() == 2){
			p.setTotalCount(((Integer)resList.get(0)));
			p.setData((List)resList.get(1));
		}
		
		return "myAudit";
	}
	


	
}
